package com.leetao.wxmsg.framework.manage;

import cn.hutool.core.text.CharSequenceUtil;
import com.alibaba.fastjson.JSON;
import com.leetao.wxmsg.common.utils.HttpUtils;
import com.leetao.wxmsg.framework.config.WeiXinConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;

/**
 * @author 菅江晖
 * @date 2021/3/8 - 12:10
 */
@Component
public class WxMsgManager {
    private final static String KEY = "accessToken";
    public final static String JSON_TAG = "JSON";


    private final static String[] ACCESS_API = {"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}","GET"};

    private final WeiXinConfig weiXinConfig;

    @Autowired
    public WxMsgManager(WeiXinConfig weiXinConfig) {
        this.weiXinConfig = weiXinConfig;
    }

    /**
     * 微信 访问token map
     */
    HashMap<String,AccessToken> accessTokenMap = new HashMap<>();

    /**
     * AccessResp
     */
    private static class AccessResp {
        String accessToken;
        String expiresIn;

        public String getAccessToken() {
            return accessToken;
        }

        public void setAccessToken(String accessToken) {
            this.accessToken = accessToken;
        }

        public String getExpiresIn() {
            return expiresIn;
        }

        public void setExpiresIn(String expiresIn) {
            this.expiresIn = expiresIn;
        }

        @Override
        public String toString() {
            return "AccessResp{" + "accessToken='" + accessToken + '\'' +
                    ", expiresIn='" + expiresIn + '\'' +
                    '}';
        }
    }

    /**
     * 访问令牌
     */
    private static final class AccessToken {

        String accessTokenString;

        LocalDateTime dateTime;


        public String getAccessTokenString() {
            return accessTokenString;
        }

        public void setAccessTokenString(String accessTokenString) {
            this.accessTokenString = accessTokenString;
        }

        public LocalDateTime getDateTime() {
            return dateTime;
        }

        public void setDateTime(LocalDateTime dateTime) {
            this.dateTime = dateTime;
        }
    }

    /**
     * 获取token
     * @return access token
     */
    public String getAccessToken() {

        AccessToken accessToken = accessTokenMap.get(KEY);
        // 过期处理
        boolean condition =  (accessToken == null);
        // 当前的时间在过期的时间之后说明已过期
        condition = condition || (LocalDateTime.now().isAfter(accessToken.getDateTime()));
        if(condition) {
            //锁
            synchronized (WxMsgManager.class) {
                if(accessToken == null || (LocalDateTime.now().isAfter(accessToken.getDateTime()))) {

                    //添加token
                    String json = HttpUtils.httpRequest(CharSequenceUtil.format(ACCESS_API[0], weiXinConfig.getAppId(), weiXinConfig.getAppSecret()), ACCESS_API[1], null, JSON_TAG);

                    AccessResp accessResp = JSON.parseObject(json, AccessResp.class);
                    AccessToken accessToken1 = new AccessToken();
                    accessToken1.setDateTime(LocalDateTime.now().plusHours(2L).minusMinutes(10L));
                    accessToken1.setAccessTokenString(accessResp.getAccessToken());

                    accessTokenMap.put(KEY,accessToken1);
                }

            }
        }

        return  accessTokenMap.get(KEY).getAccessTokenString();
    }











}
